package edu.rakovic.elearning.pages;

import java.util.Collection;

import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.ioc.annotations.Inject;

import edu.rakovic.elearning.enums.Role;
import edu.rakovic.elearning.interfaces.Application;
import edu.rakovic.elearning.interfaces.Course;
import edu.rakovic.elearning.interfaces.User;
import edu.rakovic.elearning.service.Applications;
import edu.rakovic.elearning.service.Courses;
import edu.rakovic.elearning.service.Users;

public class ViewUsers {
	
	@Inject
	@Property
	private Courses courses;
	
	@Inject
	@Property
	private Applications applications;
	
	@Inject
	@Property
	private Users users;
	
	@Property
	private User currentUser;
	
	//login check
	@SessionState
	private User user;

	private boolean userExists;
	
	public User getUser() {
		return user;
	}
	
	Object onActivate() {
		if (!userExists) return Login.class;
		if (userExists && !isUserAdmin()) return Index.class;
		return null;
	}
	
	// Event handler for action link - delete user
	Object onActionFromDelete(String userName){
		if(!user.getUserName().equals(userName)){
			User userDelete = users.retreiveSingleUser(userName);
			if(userDelete.getRole() == Role.TEACHER){
				userDelete.removeTeacher(users, courses, applications);
				}else
					userDelete.removeUser(users, courses, applications);
			
		} else return Index.class;
		return null;
	}
	
	public boolean isUserAdmin(){
		return (getUser().getRole()== Role.ADMINISTRATOR);
	}

}
